chrome.runtime.onInstalled.addListener(function () {
  console.log("插件已被安装");
});

// 创建自定义菜单项
chrome.runtime.onInstalled.addListener(function () {
  chrome.contextMenus.create({
    id: "copySelector",
    title: "Copy Selector",
    contexts: [
      "page",
      "frame",
      "selection",
      "link",
      "editable",
      "image",
      "video",
      "audio",
    ],
    documentUrlPatterns: ["*://*/*"],
  });

  chrome.contextMenus.create({
    id: "copyXpath",
    title: "Copy Xpath",
    contexts: ["all"],
  });

  chrome.contextMenus.create({
    id: "copyPageXpath",
    title: "copy PageXpath",
    contexts: ["all"],
  });

  // 添加更多自定义菜单项...
});

chrome.contextMenus.onClicked.addListener(function (info, tab) {
  // console.log(info, tab);
  if (info.menuItemId === "copyXpath") {
    chrome.tabs.sendMessage(tab.id, { action: "copyXpath" });
  } else if (info.menuItemId === "copySelector") {
    chrome.tabs.sendMessage(tab.id, { action: "copySelector" });
  } else if (info.menuItemId === "copyPageXpath") {
    chrome.tabs.sendMessage(tab.id, { action: "copyPageXpath" });
  }
});

// 接收来自 content.js 的消息
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
  if (message.action === "sendXpath") {
    var xpath = message.xpath;
    console.log("XPath:", xpath);
  } else if (message.action === "sendSelector") {
    // 在控制台输出选择器和XPath
    var selector = message.selector;
    console.log("Selector: " + selector);
  }
});
